package ua.kharkov.knure.pereverziev.diploma.web.command;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import ua.kharkov.knure.pereverziev.diploma.db.DAOServices.ServiceFactory;
import ua.kharkov.knure.pereverziev.diploma.db.entity.User;

public class GetReport implements Command {

	@Override
	public String execute(final HttpServletRequest request,
			final HttpServletResponse response) throws IOException,
			ServletException {

		final Map<String, String> parameters = new HashMap<String, String>();
		parameters.put("name", "Anton");

		final List<User> list = ServiceFactory.getInstance().getUserService()
				.getList(new User());

		System.out.println(list.size());

		final List<Map<String, ?>> maps = new ArrayList<Map<String, ?>>();
		for (final User user : list) {
			System.out.println(user);
			final Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", user.getName());
			map.put("surname", user.getSurname());
			map.put("email", user.getEmail());
			map.put("date", user.getRegistrationDate() == null ? "" : user
					.getRegistrationDate().toString());
			maps.add(map);
		}

		final JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(
				maps);

		final JasperReport jasperReport = (JasperReport) request.getSession()
				.getServletContext().getAttribute("report");

		JasperPrint jasperPrint = null;
		try {
			jasperPrint = JasperFillManager.fillReport(jasperReport,
					parameters, dataSource);
		} catch (final JRException e) {
			e.printStackTrace();
		}

		final StringWriter writer = new StringWriter();
		try {
			// response.setContentType("text/html");
			final JRHtmlExporter htmlExporter = new JRHtmlExporter();
			htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT,
					jasperPrint);
			htmlExporter.setParameter(
					JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
					Boolean.FALSE);
			htmlExporter.setParameter(
					JRHtmlExporterParameter.IGNORE_PAGE_MARGINS, Boolean.TRUE);
			htmlExporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER,
					"<a> Return to previous page</a>");
			htmlExporter.setParameter(JRHtmlExporterParameter.HTML_HEADER,
					"<a> Return to previous page</a>");
			htmlExporter.setParameter(
					JRHtmlExporterParameter.CHARACTER_ENCODING, "iso-8859-15");
			htmlExporter
					.setParameter(JRExporterParameter.OUTPUT_WRITER, writer);
			htmlExporter.exportReport();
			writer.flush();
			writer.close();

		} catch (final JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return writer.toString();
	}

	private String getRandomString() {
		return UUID.randomUUID().toString();
	}

}
